{{extend "base.html"}}

{{block head}}

<script type="text/javascript" charset="utf-8">
var treegrid;
var iconData;
$(function () {

    iconData = [
        {
            value:'',
            text:'默认'
        },
        {
            value:'icon-add',
            text:'icon-add'
        },
        {
            value:'icon-edit',
            text:'icon-edit'
        },
        {
            value:'icon-remove',
            text:'icon-remove'
        },
        {
            value:'icon-save',
            text:'icon-save'
        },
        {
            value:'icon-cut',
            text:'icon-cut'
        },
        {
            value:'icon-ok',
            text:'icon-ok'
        },
        {
            value:'icon-no',
            text:'icon-no'
        },
        {
            value:'icon-cancel',
            text:'icon-cancel'
        },
        {
            value:'icon-reload',
            text:'icon-reload'
        },
        {
            value:'icon-search',
            text:'icon-search'
        },
        {
            value:'icon-print',
            text:'icon-print'
        },
        {
            value:'icon-help',
            text:'icon-help'
        },
        {
            value:'icon-undo',
            text:'icon-undo'
        },
        {
            value:'icon-redo',
            text:'icon-redo'
        },
        {
            value:'icon-back',
            text:'icon-back'
        },
        {
            value:'icon-sum',
            text:'icon-sum'
        },
        {
            value:'icon-tip',
            text:'icon-tip'
        }
    ];

    treegrid = $('#treegrid').treegrid({
        url:'/menupost/menugraph',
        toolbar:[
            {
                text:'展开',
                iconCls:'icon-redo',
                handler:function () {
                    var node = treegrid.treegrid('getSelected');
                    if (node) {
                        treegrid.treegrid('expandAll', node.cid);
                    } else {
                        treegrid.treegrid('expandAll');
                    }
                }
            },
            '-',
            {
                text:'折叠',
                iconCls:'icon-undo',
                handler:function () {
                    var node = treegrid.treegrid('getSelected');
                    if (node) {
                        treegrid.treegrid('collapseAll', node.cid);
                    } else {
                        treegrid.treegrid('collapseAll');
                    }
                }
            },
            '-',
            {
                text:'刷新',
                iconCls:'icon-reload',
                handler:function () {
                    treegrid.treegrid('reload');
                }
            },
            '-',
            {
                text:'增加',
                iconCls:'icon-add',
                handler:function () {
                    append();
                }
            },
            '-',
            {
                text:'删除',
                iconCls:'icon-remove',
                handler:function () {
                    remove();
                }
            },
            '-',
            {
                text:'编辑',
                iconCls:'icon-edit',
                handler:function () {
                    edit();
                }
            },
            '-',
            {
                text:'取消选中',
                iconCls:'icon-undo',
                handler:function () {
                    treegrid.treegrid('unselectAll');
                }
            },
            '-',
            {
                text:'添加图形设备',
                iconCls:'icon-server-add',
                handler:function () {
                    appendgraphdevice();
                }
            },
            '-'
        ],
        title:'',
        iconCls:'icon-save',
        fit:true,
        fitColumns:true,
        nowrap:false,
        animate:false,
        border:false,
        idField:'id',
        treeField:'menuname',
        frozenColumns:[
            [
                {
                    title:'id',
                    field:'id',
                    width:150,
                    hidden:true
                },
                {
                    field:'menuname',
                    title:'菜单名称',
                    width:180,
                    formatter:function (value) {
                        if (value) {
                            return sy.fs('<span title="{0}">{1}</span>', value, value);
                        }
                    }
                }
            ]
        ],
        columns:[
            [
                {
                    field:'menuicon',
                    title:'菜单图标',
                    width:50,
                    formatter:function (value) {
                        if (!value) {
                            return '';
                        } else {
                            return sy.fs('<span class="{0}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>', value);
                        }
                    }
                },
                {
                    field:'menuurl',
                    title:'菜单地址',
                    width:250,
                    formatter:function (value) {
                        if (value) {
                            return sy.fs('<span title="{0}">{1}</span>', value, value);
                        }
                    }
                },
                {
                    field:'menuseq',
                    title:'排序',
                    width:40
                },
                {
                    field:'pid',
                    title:'上级菜单ID',
                    width:150,
                    hidden:true
                },
                {
                    field:'pname',
                    title:'上级菜单',
                    width:150
                }
            ]
        ],
        onLoadSuccess:function (row, data) {
            /*var t = $(this);
             if (data) {
             $(data).each(function(index, d) {
             if (this.state == 'closed') {
             t.treegrid('expandAll');
             }
             });
             }*/
        },
        onExpand:function (row) {
            treegrid.treegrid('unselectAll');
        },
        onCollapse:function (row) {
            treegrid.treegrid('unselectAll');
        }
    });

});

function edit() {
    var node = treegrid.treegrid('getSelected');
    if (node) {
        var p = parent.sy.dialog({
            title:'编辑菜单',
            href:'/menu/menugraphedit',
            width:460,
            height:220,
            buttons:[
                {
                    text:'编辑',
                    handler:function () {
                        var f = p.find('form');
                        f.form('submit', {
                            url:'/menu/menugraphsave',
                            success:function (d) {
                                var json = $.parseJSON(d);
                                if (json.success) {
                                    treegrid.treegrid('reload');
                                    p.dialog('close');
                                    parent.graphTree.tree('reload');
                                    /*刷新左侧菜单树*/
                                }
                                parent.sy.messagerShow({
                                    msg:json.msg,
                                    title:'提示'
                                });
                            }
                        });
                    }
                }
            ],
            onLoad:function () {
                var f = p.find('form');
                var pid = f.find('input[name=pid]');
                var iconCls = f.find('input[name=menuicon]');
                var iconCombo = iconCls.combobox({
                    data:iconData,
                    formatter:function (v) {
                        return sy.fs('<span class="{0}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>{1}', v.value, v.value);
                    }
                });
                var ptree = pid.combotree({
                    lines:true,
                    url:'/menupost/menugraphinfo',
                    onLoadSuccess:function () {
                        parent.$.messager.progress('close');
                    }
                });
                f.form('load', node);
            }
        });
    } else {
        parent.sy.messagerAlert('提示', '请选中要编辑的记录！', 'error');
    }
}
function append() {
    var p = parent.sy.dialog({
        title:'添加菜单',
        href:'/menu/menugraphadd',
        width:460,
        height:220,
        buttons:[
            {
                text:'添加',
                handler:function () {
                    var f = p.find('form');
                    f.form('submit', {
                        url:'/menu/menugraphsave',
                        success:function (d) {
                            var json = $.parseJSON(d);
                            if (json.success) {
                                treegrid.treegrid('reload');
                                p.dialog('close');
                                parent.graphTree.tree('reload');
                                /*刷新左侧菜单树*/
                            }
                            parent.sy.messagerShow({
                                msg:json.msg,
                                title:'提示'
                            });
                        }
                    });
                }
            }
        ],
        onLoad:function () {
            var f = p.find('form');
            var pid = f.find('input[name=pid]');
            var iconCls = f.find('input[name=menuicon]');
            var iconCombo = iconCls.combobox({
                data:iconData,
                formatter:function (v) {
                    return sy.fs('<span class="{0}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>{1}', v.value, v.value);
                }
            });
            var ptree = pid.combotree({
                lines:true,
                url:'/menupost/menugraphinfo'
            });
        }
    });
}
function remove() {
    var node = treegrid.treegrid('getSelected');
    if (node) {
        parent.sy.messagerConfirm('询问', '您确定要删除【' + node.menuname + '】？', function (b) {
            if (b) {
                $.ajax({
                    url:'/menu/menugraphdel',
                    data:{
                        id:node.id
                    },
                    cache:false,
                    dataType:'JSON',
                    success:function (r) {
                        if (r.success) {
                            treegrid.treegrid('remove', r.obj);
                            parent.graphTree.tree('reload');
                            /*刷新左侧菜单树*/
                        }
                        parent.sy.messagerShow({
                            msg:r.msg,
                            title:'提示'
                        });
                    }
                });
            }
        });
    }
};
function appendgraphdevice(){
/*添加要生成图形的设备*/
    var p = parent.sy.dialog({
        title:'分配图形监控组',
        href:'/menu/graphdeviceadd',
        width:800,
        height:500,
        buttons:[
            {
                text:'添加',
                handler:function () {
                    var f = p.find('form');
                    f.form('submit', {
                        url:'/menu/graphdevicesave',
                        success:function (d) {
                            var json = $.parseJSON(d);
                            if (json.success) {
                                treegrid.treegrid('reload');
                                p.dialog('close');
                                parent.graphTree.tree('reload');
                                /*刷新左侧菜单树*/
                            }
                            parent.sy.messagerShow({
                                msg:json.msg,
                                title:'提示'
                            });
                        }
                    });
                }
            }
        ],
        onLoad:function () {
            var f = p.find('form');
            var pid = f.find('input[name=pid]');
            var iconCls = f.find('input[name=menuicon]');
            var iconCombo = iconCls.combobox({
                data:iconData,
                formatter:function (v) {
                    return sy.fs('<span class="{0}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>{1}', v.value, v.value);
                }
            });
            var ptree = pid.combotree({
                lines:true,
                url:'/menupost/menugraphinfo'
            });
        }
    });
};
</script>
{{end}}

{{block bodyclass}}
<body class="easyui-layout" data-options="fit:true">
{{end}}

{{block content}}
<div data-options="region:'center',border:false" style="overflow: hidden;">
    <table id="treegrid"></table>
</div>
{{end}}